<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/12/1
 * Time: 10:20
 */

namespace app\backstage\controller;


use app\backstage\facade\NovelFacade;
use app\backstage\logic\NovelLogic;
use app\backstage\model\ManagerRole;
use app\backstage\server\NovelServer;
use app\backstage\server\UploadServer;
use app\common\classes\BehaviorRedis;
use app\common\model\FreqToClassAccess;
use app\common\model\NovelChapter;
use app\common\model\NovelClass;
use app\common\model\NovelClassSon;
use app\common\model\NovelFreq;
use app\common\model\NovelLabel;
use app\common\model\NovelToAgentAccess;
use app\common\model\NovelToLabelAccess;
use think\Controller;
use think\Db;
use think\db\Expression;
use think\facade\Env;

class Novel extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => ['searchNovels', 'preview', 'getSelectClassSon', 'renderNovels']],
    ];

    protected $logic;

    /**
     * 容器写法
     * Novel constructor.
     * @param NovelLogic|null $logic
     */
    public function __construct(NovelLogic $logic = null)
    {
        $this->logic = $logic;
        parent::__construct();
    }

    /**
     * 小说列表
     * @return mixed
     */
    public function index()
    {
        $this->assign('classArr', NovelClass::all());
        $this->assign('freqArr', NovelFreq::all());
        $freq = NovelFreq::all();
        foreach ($freq as $k => $v) {
            $v->sonClass;
        }
        $labels = NovelLabel::all();
        $this->assign('labels', $labels);
        return $this->fetch();
    }

    /**
     * 列表接口
     * @return \think\response\Json
     */
    public function list()
    {
        $id = trim(input('id'));
        $title = input('title');
        $scale = input('scale');
        $freq_id = input('freq_id');
        $class_id = input('class_id');
        $update_status = input('update_status');
        $in_shelf = input('in_shelf');
        $is_exclusive = input('is_exclusive');
        $label_id = input('label_id', 0);
        $page = input('page', 1);
        $limit = input('limit', 20);
        $where = [];
        if ($id != '') {
            $where[] = ['id', '=', $id];
        }
        if ($title != '') {
            $where[] = ['title|origin_title', 'like', "%{$title}%"];
        }
        if ($scale != '') {
            $where[] = ['scale', '=', $scale];
        }
        if ($freq_id != '') {
            $where[] = ['freq_id', '=', $freq_id];
        }
        if ($class_id != '') {
            $where[] = ['class_id', '=', $class_id];
        }
        if ($update_status != '') {
            $where[] = ['update_status', '=', $update_status];
        }
        if(strlen($in_shelf) >= 1)
        {
            $where[] = ['in_shelf', '=', $in_shelf];
        }
        if ($is_exclusive == 1) {
            $where[] = ['is_exclusive', '=', $is_exclusive];
        }
        if ($label_id != 0) {
            $novelIds = NovelToLabelAccess::where('label_id', $label_id)->column('novel_id');
            $where[] = ['id', 'in', $novelIds];
        }
        $orderBy = "id desc";
        $list = \app\common\model\Novel::getList($where, '*', $orderBy, $page, $limit, ['freq', 'novel_class']);
        foreach ($list as $k => $v) {
            $list[$k]['number_words_text'] = $v->number_words_text;
        }
        $count = \app\common\model\Novel::getCount($where);
        return sucJson($list, $count);
    }

    /**
     * 小说详情
     * @return mixed
     */
    public function info()
    {
        $id = input('id');
        $novel = \app\common\model\Novel::get($id);
        if (!$novel) {
            $this->error('小说不存在');
        }
        $this->assign('novel', $novel);

        $labels = NovelLabel::all();
        $haveLabels = array_column($novel->labels->toArray(), 'id');
        foreach ($labels as $k => $v) {
            $labels[$k]['have'] = 0;
            if (in_array($v['id'], $haveLabels)) {
                $labels[$k]['have'] = 1;
            }
        }
        $this->assign('labels', $labels);
        $freq = NovelFreq::all();
        foreach ($freq as $k => $v) {
            $v->sonClass;
        }
        $this->assign('freq', $freq);
        $this->assign('freqJson', json_encode($freq));
        /**
         * 查询子分类列表
         */
        $sonClass = $novel->optionalSonClass();
        $this->assign('sonClass', $sonClass);
        return $this->fetch();
    }

    /**
     * 添加小说页面
     * @return mixed
     */
    public function addForm()
    {
        $labels = NovelLabel::all();
        $this->assign('labels', $labels);
        $freq = NovelFreq::all();
        foreach ($freq as $k => $v) {
            $v->sonClass;
        }
        $this->assign('freq', $freq);
        $this->assign('freqJson', json_encode($freq));
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.add');
        if (true !== $result) {
            $this->error($result);
        }
        NovelFacade::setNovel($params);
        $this->success();
    }


    /**
     * 编辑书籍
     */
    public function edit()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.edit');
        if (true !== $result) {
            $this->error($result);
        }
        NovelFacade::setNovel($params, $params['id']);
        $this->success();
    }


    /**
     * 续传和覆盖章节
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function upload()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.upload');
        if (true !== $result) {
            $this->error($result);
        }
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $novel = \app\common\model\Novel::get($params['id']);
        if (!$novel) {
            $this->error('替换书籍不存在');
        }
        $info = $file->validate(['ext' => 'txt'])->move('../uploads/renewBook');
        if ($info) {
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $filepath = Env::get('root_path') . "uploads/renewBook/" . $info->getSaveName();
            unset($info);
            $uploadServer = new UploadServer();
            $novelFileContent = $uploadServer->getContentByFile($filepath);
            $novelServer = new NovelServer();
            $novelServer->batchAddChapter($novel, $params['type'], $novelFileContent);
            $this->success();
        } else {
            $this->error($file->getError());
        }
    }

    /**
     * 章节目录
     * @return \think\response\Json
     * @Author: whh
     */
    public function directory()
    {
        $nid = input('nid');
        $order = input('order', 'asc');
        $page = input('page', 1);
        $limit = input('limit', 20);
        if (!in_array($order, ['asc', 'desc'])) {
            $order = 'asc';
        }
        $where = [];
        if ($nid != '') {
            $where[] = ['novel_id', '=', $nid];
        }
        $orderBy = "chapter_no {$order}";
        $list = NovelChapter::getList($where, '*', $orderBy, $page, $limit);
        $count = NovelChapter::getCount($where);
        return sucJson($list, $count);
    }

    /**
     * 预览页
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function preview()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.preview');
        if (true !== $result) {
            $this->error($result);
        }
        $where[] = ['novel_id', '=', $params['novel_id']];
        $where[] = ['chapter_no', '=', $params['chapter_no']];
        $chapter = NovelChapter::where($where)->findOrFail();
        $info = $chapter->getHtContent();
        $maxChapterNo = NovelChapter::where("novel_id", $params['novel_id'])->max('chapter_no');
        $this->assign('maxChapterNo', $maxChapterNo);
        $this->assign('chapter', $chapter);
        $this->assign('info', $info);
        return $this->fetch();
    }


    /**
     * 章节页面
     * @return mixed|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function chapterForm()
    {
        $novel_id = input('novel_id');
        $chapter_no = input('chapter_no');
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.chapterForm');
        if (true !== $result) {
            $this->error($result);
        }
        $chapterModel = null;
        $content = '';
        if ($chapter_no != '') {
            $where[] = ['novel_id', '=', $novel_id];
            $where[] = ['chapter_no', '=', $chapter_no];
            $chapterModel = NovelChapter::where($where)->findOrFail();
            $content = $chapterModel->getHtContent()['content'];
        }
        $this->assign('cm', $chapterModel);
        $this->assign('novel_id', $novel_id);
        $this->assign('chapter_no', $chapter_no);
        $this->assign('content', $content);
        return $this->fetch();
    }


    /**
     * 设置章节
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function setChapter()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.setChapter');
        if (true !== $result) {
            $this->error($result);
        }
        $this->logic->setChapter($params);
        $this->success();
    }

    /**
     * 专属书
     * @return mixed
     */
    public function exclusive()
    {
        /**
         * 查询所有代理
         */
        $agent = ManagerRole::getRoleByAlias('agent');
        $agents = \app\backstage\model\ManagerUser::field('id,account')->where('role_id', $agent['id'])->order("id desc")->select();
        $this->assign('agents', $agents);
        /**
         * 查询已授权书籍
         */
        $this->assign('classArr', NovelClass::all());
        $this->assign('freqArr', NovelFreq::all());
        return $this->fetch();
    }

    /**
     * 格式化穿梭框所需数据
     * @param $data
     * @return false|string
     */
    private function exclusiveDataToJson($data)
    {
        $formatData = [];
        foreach ($data as $k => $v) {
            $formatData[$k]['value'] = $v['id'];
            $formatData[$k]['title'] = $v['account'];
        }
        $formatData = json_encode($formatData);
        return $formatData;
    }


    /**
     * 添加专属书页面
     * @return mixed
     */
    public function addExclusiveForm()
    {
        $agent = ManagerRole::getRoleByAlias('agent');
        $all = \app\backstage\model\ManagerUser::field('id,account')->where('role_id', $agent['id'])->order("id desc")->select();
        $all = $this->exclusiveDataToJson($all);
        $have = $this->exclusiveDataToJson([]);
        $this->assign('all', $all);
        $this->assign('have', $have);
        return $this->fetch();
    }


    /**
     * 修改专属
     * @return mixed
     */
    public function editExclusiveForm()
    {
        $nid = input('nid');
        $novel = \app\common\model\Novel::get($nid);
        if (!$novel) {
            return errJson('书籍不存在');
        }
        $agent = ManagerRole::getRoleByAlias('agent');
        $all = \app\backstage\model\ManagerUser::field('id,account')->where('role_id', $agent['id'])->order("id desc")->select();
        $access = new NovelToAgentAccess();
        $have = $access->where('nid', $nid)->column('agent_id');
        $all = $this->exclusiveDataToJson($all);
        $have = json_encode($have);
        $this->assign('novel', $novel);
        $this->assign('all', $all);
        $this->assign('have', $have);
        return $this->fetch();
    }


    /**
     * 检索小说
     * @return \think\response\Json
     */
    public function searchNovels()
    {
        $key = input('key');
        $page = input('page', 1);
        $limit = 10;
        $novel = new \app\common\model\Novel();
        if ($key != '') {
            if (strpos($key, '[') !== false && strpos($key, '] ') !== false) {
                $keyArr = explode('] ', $key);
                $key = $keyArr[1];
            }
            $query = $novel->where(function ($query) use ($key) {
                $query->where('id', $key)
                    ->whereOr('title', 'like', "%$key%");
            });
        } else {
            $query = $novel;
        }
        $role = request()->_role;
        $user = request()->_user;
        $query->where(function ($query) use ($role, $user) {
            /**
             * 专属书籍
             */
            if ($role->alias == 'agent' || $role->alias == 'channel') {
                if ($role->alias == 'channel') {
                    $novelIds = NovelToAgentAccess::where("agent_id", $user->parent_id)->column('nid');
                } else {
                    $novelIds = NovelToAgentAccess::where("agent_id", $user->id)->column('nid');
                }
                $query->where('is_exclusive', 0)
                    ->whereOr('id', 'in', $novelIds);
            }
        });
        $data = $query->field("id value,title input_name")->order("id desc")->page($page, $limit)->select();
        $count = $query->count();
        foreach ($data as $k => $v) {
            $data[$k]['name'] = "[{$v['value']}] " . $v['input_name'];
        }
        return sucJson($data, intval($count));
    }


    /**
     * 专属书籍列表
     * @return \think\response\Json
     * @Author: whh
     */
    public function exclusiveNovelList()
    {
        $id = trim(input('id'));
        $title = input('title');
        $scale = input('scale');
        $freq_id = input('freq_id');
        $class_id = input('class_id');
        $update_status = input('update_status');
        $agent_id = input('agent_id');
        $page = input('page', 1);
        $limit = input('limit', 20);
        $where = [];
        if ($id != '') {
            $where[] = ['id', '=', $id];
        }
        if ($title != '') {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if ($scale != '') {
            $where[] = ['scale', '=', $scale];
        }
        if ($freq_id != '') {
            $where[] = ['freq_id', '=', $freq_id];
        }
        if ($class_id != '') {
            $where[] = ['class_id', '=', $class_id];
        }
        if ($update_status != '') {
            $where[] = ['update_status', '=', $update_status];
        }
        if ($agent_id != '') {
            $nIds = NovelToAgentAccess::where("agent_id", $agent_id)->column('nid');
            $where[] = ['id', 'in', $nIds];
        }
        $where[] = ['is_exclusive', '=', 1];
        $orderBy = "update_time desc";
        $list = \app\common\model\Novel::getList($where, '*', $orderBy, $page, $limit, ['freq', 'novel_class', 'agents']);
        foreach ($list as $k => $v) {
            $list[$k]['agents_count'] = count($v['agents']);
        }
        $count = \app\common\model\Novel::getCount($where);
        return sucJson($list, $count);
    }


    /**
     * 添加专属书籍
     * @Author: whh
     */
    public function setExclusive()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.setExclusive');
        if (true !== $result) {
            $this->error($result);
        }
        return NovelFacade::setExclusive($params['ex_nid'], $params['agents'], $params['is_add']);
    }

    /**
     * 移除专属
     * @return \think\response\Json
     */
    public function delExclusive()
    {
        $nid = input('nid');
        $novel = \app\common\model\Novel::get($nid);
        if (!$novel) {
            return errJson('书籍不存在');
        }
        $novel->agents()->detach();
        $novel->is_exclusive = 0;
        $novel->save();
        $this->success();
    }


    /**
     * 标签页
     * @return mixed
     */
    public function labelView()
    {
        return $this->fetch();
    }


    /**
     * 标签列表
     * @return \think\response\Json
     */
    public function labels()
    {
        $keyword = input('keyword');
        $page = input('page', 1);
        $limit = input('limit', 20);
        $where = [];
        if ($keyword != '') {
            $where[] = ['id|name', '=', $keyword];
        }
        $orderBy = "id desc";
        $list = NovelLabel::getList($where, '*', $orderBy, $page, $limit);
        foreach ($list as $k => $v) {
            $list[$k]['novel_count'] = count($v['novels']);
        }
        $count = NovelLabel::getCount($where);
        return sucJson($list, $count);
    }


    /**
     * 设置标签
     * @return mixed
     */
    public function setLabel()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.setLabel');
        if (true !== $result) {
            $this->error($result);
        }
        $id = isset($params['id']) && $params['id'] ? $params['id'] : null;
        return NovelFacade::setLabel($params['name'], $id);
    }


    /**
     * 删除标签
     * @throws \Exception
     */
    public function delLabel()
    {
        $id = input('id');
        $label = NovelLabel::get($id);
        if (!$label) {
            $this->error('标签不存在');
        }
        $label->novels()->detach();
        $label->delete();
        $this->success();
    }


    /**
     * 分类管理
     * @return mixed
     */
    public function classManage()
    {
        $allFreq = NovelFreq::all();
        $allClass = NovelClass::all();
        $this->assign('allFreq', $allFreq);
        $this->assign('allClass', $allClass);
        return $this->fetch();
    }

    /**
     * 分类列表
     * @return \think\response\Json
     */
    public function freqList()
    {
        $page = input('page', 1);
        $limit = input('limit', 20);
        $where = [];
        $logic = new NovelLogic();
        return $logic->freqList($where, $page, $limit);
    }

    /**
     * 设置频道
     * @return mixed
     * @Author: whh
     */
    public function setFreq()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.setFreq');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = new NovelLogic();
        if (isset($params['id'])) {
            return $logic->setFreq($params['name'], $params['id']);
        }
        return $logic->setFreq($params['name']);
    }


    /**
     * 删除频道
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delFreq()
    {
        $id = input('id');
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.delFreq');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = new NovelLogic();
        return $logic->delFreq($id);
    }


    /**
     * 频道列表
     * @return \think\response\Json
     * @Author: whh
     */
    public function classList()
    {
        $name = input('name');
        $freqId = input('freq_id');
        $page = input('page', 1);
        $limit = input('limit', 20);
        $where = [];
        if ($name != '') {
            $where[] = ['name', 'like', "%$name%"];
        }
        if ($freqId != '') {
            $classIdArr = FreqToClassAccess::where("freq_id", $freqId)->column('class_id');
            $where[] = ['id', 'in', $classIdArr];
        }
        $logic = new NovelLogic();
        $orderBy = 'wgt desc,id desc';
        return $logic->classList($where, $page, $limit, $orderBy);
    }


    /**
     * 设置分类
     * @return \think\response\Json
     */
    public function setClass()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.setClass');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = new NovelLogic();
        return $logic->setClass($params);
    }


    /**
     * 删除分类
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delClass()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.delClass');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = new NovelLogic();
        return $logic->delClass($params['id']);
    }

    /**
     * 子分类列表
     * @return \think\response\Json
     * @Author: whh
     */
    public function classSonList()
    {
        $name = input('name');
        $freqId = input('freq_id');
        $classId = input('class_id');
        $page = input('page', 1);
        $limit = input('limit', 20);
        $logic = new NovelLogic();
        $where = [];
        if ($name != '') {
            $where[] = ['name', 'like', "%$name%"];
        }
        if ($freqId != '') {
            $where[] = ['freq_id', '=', $freqId];
        }
        if ($classId != '') {
            $where[] = ['class_id', '=', $classId];
        }
        return $logic->classSonList($where, $page, $limit);
    }

    /**
     * 设置分类
     * @return \think\response\Json
     * @Author: whh
     */
    public function setClassSon()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.setClassSon');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = new NovelLogic();
        return $logic->setClassSon($params);
    }

    /**
     * 删除分类
     * @return \think\response\Json
     * @throws \Exception
     */
    public function delClassSon()
    {
        $id = input('id');
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.delClassSon');
        if (true !== $result) {
            $this->error($result);
        }
        $logic = new NovelLogic();
        return $logic->delClassSon($id);
    }


    /**
     * 获取可选的子分类
     * @return \think\response\Json
     */
    public function getSelectClassSon()
    {
        $freqId = input('freq_id');
        $classId = input('class_id');
        if ($freqId == '' || $classId == '') {
            return sucJson();
        }
        $where[] = ['freq_id', '=', $freqId];
        $where[] = ['class_id', '=', $classId];
        $list = NovelClassSon::where($where)->select();
        return sucJson($list);
    }


    /**
     * 设置分类权限
     * @return \think\response\Json
     */
    public function setClassWgt()
    {
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.setClassWgt');
        if (true !== $result) {
            $this->error($result);
        }
        NovelClass::where('id', $params['id'])->setField('wgt', $params['wgt']);
        return sucJson();
    }


    /**
     * 渲染书籍
     * @return \think\response\Json
     * @Author: whh
     */
    public function renderNovels(){
        $params = request()->param();
        $result = $this->validate($params, 'app\backstage\validate\NovelValidate.renderNovels');
        if (true !== $result) {
            $this->error($result);
        }
        $nidArr = input('nidArr');
        $novel = new \app\common\model\Novel();
        $where[] = ['id','in',$nidArr];
        $where[] = ['in_shelf', '=', 1];

        $query = $novel->where($where);
        $role = request()->_role;
        $user = request()->_user;
        $query->where(function ($query) use ($role, $user) {
            /**
             * 专属书籍
             */
            if ($role->alias == 'agent' || $role->alias == 'channel') {
                if ($role->alias == 'channel') {
                    $novelIds = NovelToAgentAccess::where("agent_id", $user->parent_id)->column('nid');
                } else {
                    $novelIds = NovelToAgentAccess::where("agent_id", $user->id)->column('nid');
                }
                $query->where('is_exclusive', 0)
                    ->whereOr('id', 'in', $novelIds);
            }
        });
        $order = '';
        if ($nidArr) {
            $order = new Expression('field(id,' . implode(',', $nidArr) . ')');
        }
        $data = $query->field("id,title,scale,in_shelf,freq_id,class_id")->with(['freq', 'novel_class'])->order($order)->select()->toArray();
        $count = count($data);
        $type = input('type');
        if ($type == 'open') {
            if ($count > 1) {
                return errJson('App开屏最多设置一本');
            }
        }elseif ($type == 'banner'){
            if ($count > 1) {
                return errJson('一个banner只能设置一本书');
            }
        }elseif ($type == 'plBanner'){
            $data = array_reverse($data);
        }
        if ($count > 10){
            return errJson('最多添加10本');
        }
        if ($count == 0){
            return errJson('书籍不存在或已添加');
        }
        return sucJson($data);
    }

    /**
     * 书籍分析
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @Author: whh
     */
    public function dataAnalysis()
    {
        if (request()->isAjax()) {
            $page = input('page', 1);
            $limit = input('limit', 10);
            $nid = input('nid');
            $start_date = input('start_date');
            $end_date = input('end_date');
            $field = input('field','uv');
            $order = input('order','desc');
            $orderBy = "{$field} {$order}";
            $where = [];
            if ($nid != '') {
                $where[] = ['nid', '=', $nid];
            }
            if ($start_date != '') {
                $where[] = ['read_date', '>=', $start_date];
            }
            if ($end_date != '') {
                $where[] = ['read_date', '<=', $end_date];
            }
            return $this->logic->dataAnalysisList($where, $page, $limit, $orderBy);
        }
        return $this->fetch();
    }

    /**
     * 书籍每日数据
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @Author: whh
     */
    public function dataDetail(){
        $nid = input('id');
        if (!$nid) {
            $this->error('参数异常');
        }
        if (request()->isAjax()) {
            $page = input('page', 1);
            $limit = input('limit', 10);
            $start_date = input('start_date');
            $end_date = input('end_date');
            $field = input('field','read_date');
            $order = input('order','desc');
            $orderBy = "{$field} {$order}";
            $where[] = ['nid', '=', $nid];
            if ($start_date != '') {
                $where[] = ['read_date', '>=', $start_date];
            }
            if ($end_date != '') {
                $where[] = ['read_date', '<=', $end_date];
            }
            return $this->logic->dataDetail($nid, $where, $page, $limit, $orderBy);
        }
        $novel = \app\common\model\Novel::where('id', '=', $nid)->findOrFail();
        $this->assign('nid', $nid);
        $this->assign('novel', $novel);
        return $this->fetch();
    }

    /**
     * 复充分析
     * @return mixed
     * @Author: whh
     */
    public function againFx(){
        $nid = input('id');
        if (!$nid) {
            $this->error('参数异常');
        }
        $where = [];
        $list = $this->logic->againFxList($nid, $where);
        $user = request()->_user;
        $qas = getMyQas($user);
        $novel = \app\common\model\Novel::where('id', '=', $nid)->findOrFail();
        $this->assign('qas', $qas);
        $this->assign('nid', $nid);
        $this->assign('novel', $novel);
        $this->assign('list', $list);
        return $this->fetch();
    }
}